package com.onjob.employment;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.onjob.administer.City;
import com.onjob.administer.CityDao;
import com.onjob.administer.Status;
import com.onjob.administer.StatusDao;
import com.onjob.photo.Photo;
import com.onjob.photo.PhotoDao;
import com.onjob.security.Users;
import com.onjob.security.UsersDao;
import com.opensymphony.xwork2.ActionSupport;

public class PersonInfoAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private PersonInfo personInfo;
	private PersonInfoDao personInfoDao;
	private CityDao cityDao;
	private StatusDao statusDao;
	public List<City> citylist;
	public List<Status> statusList;
	private City city;
	private Status statuss;
	private Users user;
	private UsersDao userDao;
	private Long userId;
	private Photo photos;
	private PhotoDao photoDao;
	private Resume resumes;
	private ResumeDao resumeDao;
	private InputStream fileInputStream;

	public void setPersonInfo(PersonInfo personInfo) {
		this.personInfo = personInfo;
	}

	public void setPersonInfoDao(PersonInfoDao personInfoDao) {
		this.personInfoDao = personInfoDao;
	}

	public List<City> getCitylist() {

		if (null == this.citylist) {

			citylist = cityDao.findAll();
		}
		return citylist;
	}

	public void setCitylist(List<City> citylist) {

		this.citylist = citylist;
	}

	public List<Status> getStatusList() {
		if (null == this.statusList) {

			statusList = statusDao.findAll();
		}
		return statusList;
	}

	public void setStatusList(List<Status> statusList) {
		this.statusList = statusList;
	}

	public City getCity() {
		return city;
	}

	public void setCity(City city) {
		this.city = city;
	}

	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}

	public void setStatusDao(StatusDao statusDao) {
		this.statusDao = statusDao;
	}

	public PersonInfo getPersonInfo() {
		return personInfo;
	}

	public PersonInfoDao getPersonInfoDao() {
		return personInfoDao;
	}

	public CityDao getCityDao() {
		return cityDao;
	}

	public StatusDao getStatusDao() {
		return statusDao;
	}

	// 测验程序
	@Transactional(propagation = Propagation.REQUIRED)
	public String qiantai() throws Exception {
		City c = new City();
		c.setCityName("北京");
		cityDao.create(c);
		Status s = new Status();
		s.setStatusName("本科");
		statusDao.create(s);
		Users users = new Users();
		users.setUserName("aaa");
		users.setUserPassword("aaa");
		users.setUserEmail("sdfsdf");
		userDao.create(users);
		ServletActionContext.getRequest().getSession().setAttribute("usersign",
				1L);
		return SUCCESS;
	}

	public String register() throws Exception {

		return SUCCESS;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public String submits() throws Exception {
		// if(null !=
		// this.personInfoDao.findByType((((Users)ServletActionContext.getRequest()
		// .getSession().getAttribute("user")).getId()).toString(),
		// "userId.id")){
		// this.personInfo.setUserId(this.userDao
		// .findById(((Users)ServletActionContext.getRequest()
		// .getSession().getAttribute("user")).getId()));
		// this.personInfoDao.update(this.personInfo);
		// }else{
		this.personInfo.setUserId(this.userDao
				.findById(((Users) ServletActionContext.getRequest()
						.getSession().getAttribute("user")).getId()));

		this.personInfoDao.saveOrUpdate(personInfo);
		// }

		System.out.println(this.personInfo.getId());
		ServletActionContext.getRequest().getSession().setAttribute(
				"personInfoid",
				((this.personInfoDao.findByTypes(this.personInfo
						.getPersonName(), ((Users) ServletActionContext
						.getRequest().getSession().getAttribute("user"))
						.getId().toString(), "personName", "userId.id"))
						.getId()));
		return SUCCESS;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public String personInfo_update() throws Exception {

		this.personInfo = this.personInfoDao.findById(ServletActionContext
				.getRequest().getSession().getAttribute("personInfoid")
				.toString());
		this.setUserId(this.personInfo.getUserId().getId());
		return SUCCESS;
	}

	public void setUserDao(UsersDao userDao) {
		this.userDao = userDao;
	}

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public Status getStatuss() {
		return statuss;
	}

	public void setStatuss(Status statuss) {
		this.statuss = statuss;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	/*
	 * 
	 * @用户查看个人信息的处理
	 */

	public String employment_index() throws Exception {

		return SUCCESS;
	}

	/*
	 * 处理个人信息的显示通过session得到user的id
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public String person_message() throws Exception {
		this.personInfo = this.personInfoDao
				.findByUserId(((Users) ServletActionContext.getRequest()
						.getSession().getAttribute("user")).getId());
//		this.photos = this.photoDao.findByResumeId((this.resumeDao.findResumeByType(this.personInfo.getId(),"personInfo.id")).getId());
		System.out.println("---------------------"
				+ this.personInfo.getPersonAddress());
		System.out.println("---------------------"
				+ this.photos);
		
		//处理在没有图片的显示效果
			
		
		return SUCCESS;
	}
	/*
	 * 显示图片
	 */
	public Photo getPhotos() {
		return photos;
	}

	public void setPhotos(Photo photos) {
		this.photos = photos;
	}

	public void setPhotoDao(PhotoDao photoDao) {
		this.photoDao = photoDao;
	}

	public Resume getResumes() {
		return resumes;
	}

	public void setResumes(Resume resumes) {
		this.resumes = resumes;
	}

	public void setResumeDao(ResumeDao resumeDao) {
		this.resumeDao = resumeDao;
	}

	public InputStream getFileInputStream() {
		return fileInputStream;
	}

	public void setFileInputStream(InputStream fileInputStream) {
		this.fileInputStream = fileInputStream;
	}

}
